/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.exception;


/**
 *  Chained exception which allows to keep "stack trace"
 *  when exception is thrown.
 *
 */
public class ChainedException extends Exception
{
    /** cause of the exception. */
    private Throwable cause = null;

    /**
     * Class constructor.
     */
    public ChainedException()
    {
        super();
    }

    /**
     * Builds a ChainedException with a message.
     *
     * @param message message
     */
    public ChainedException(String message)
    {
        super(message);
    }

    /**
     * Builds a ChainedException with a message and a cause.
     *
     * @param message message
     * @param acause cause
     */
    public ChainedException(String message, Throwable acause)
    {
        super(message);

        this.cause = acause;
    }

    /** Gets cause.
    * @return cause
    */
    @Override
	public Throwable getCause()
    {
        return cause;
    }

    /** Displays stack trace with cause.
     *
     */
    @Override
	public void printStackTrace()
    {
        super.printStackTrace();

        if (cause != null)
        {
            System.err.println("Caused by:");
            cause.printStackTrace();
        }
    }

    /** Displays stack trace with cause in specified stream.
     *  @param ps print stream
     */
    @Override
	public void printStackTrace(java.io.PrintStream ps)
    {
        super.printStackTrace(ps);

        if (cause != null)
        {
            ps.println("Caused by:");
            cause.printStackTrace(ps);
        }
    }

    /** Displays stack trace with cause in specified writer.
     *  @param pw print writer
     */
    @Override
	public void printStackTrace(java.io.PrintWriter pw)
    {
        super.printStackTrace(pw);

        if (cause != null)
        {
            pw.println("Caused by : ");
            cause.printStackTrace(pw);
        }
    }
}
